﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace StrumDiagramMaker
{
    class StrumPattern
    {
        //Name of strum pattern
        private string title = "";

        //Get/Set title.
        public string GetTitle()
        {
            return title;
        }
        public void SetTitle(string newTitle, char delimiter)
        {
            //If there are any commas in the title, remove them. They will cause problems when opening the pattern.
            if (newTitle.Contains(delimiter.ToString()))
                newTitle = newTitle.Replace(delimiter.ToString(), "");

            title = newTitle;
        }

        //Name of author
        private string author = "";

        //Get/Set author.
        public string GetAuthor()
        {
            return author;
        }
        public void SetAuthor(string newAuthor, char delimiter)
        {
            //If there are any commas in the author name, remove them. They will cause problems when opening the pattern.
            if (newAuthor.Contains(delimiter.ToString()))
                newAuthor = newAuthor.Replace(delimiter.ToString(), "");

            author = newAuthor;
        }

        //Number of beats in each measure. 3 means 3/4 time, 4 means 4/4 time.
        private int beatsPerMeasure = 4;

        //Strum duration. 8 means 8th notes, while 16 means 16th notes.
        private int strumDuration = 16;

        //Beat strum values. 1 means strum on that beat.
        //Beat 1
        private int _1 = 1;
        private int _1e = 1;
        private int _1and = 1;
        private int _1a = 1;

        //Beat 2
        private int _2 = 1;
        private int _2e = 1;
        private int _2and = 1;
        private int _2a = 1;

        //Beat 3
        private int _3 = 1;
        private int _3e = 1;
        private int _3and = 1;
        private int _3a = 1;

        //Beat 4
        private int _4 = 1;
        private int _4e = 1;
        private int _4and = 1;
        private int _4a = 1;

        //Change time signature
        public void SetBeatsPerMeasure(int newBeatsPerMeasure)
        {
            //If the new value is not 8 or 16, display and error message.
            if (newBeatsPerMeasure != 3 && newBeatsPerMeasure != 4)
            {
                MessageBox.Show("Invalid number of beats per measure.");
            }
            //If the new value IS a valid one, set the beatsPerMeasure value equal to the new value.
            else
            {
                beatsPerMeasure = newBeatsPerMeasure;
            }

        }

        //Change strum duration.
        public void SetStrumDuration(short newStrumDuration)
        {
            if (newStrumDuration == 8)
                strumDuration = 8;
            else if (newStrumDuration == 16)
                strumDuration = 16;
        }

        //Toggle strum yes/no
        public void ToggleStrum(string strumName)
        {
            switch (strumName)
            {
                //Toggle the appropriate strum arrow on/off.
                //Beat one.
                case "1":
                    if (_1 == 0)
                        _1 = 1;
                    else
                        _1 = 0;
                    break;

                case "1e":
                    if (_1e == 0)
                        _1e = 1;
                    else
                        _1e = 0;
                    break;

                case "1and":
                    if (_1and == 0)
                        _1and = 1;
                    else
                        _1and = 0;
                    break;

                case "1a":
                    if (_1a == 0)
                        _1a = 1;
                    else
                        _1a = 0;
                    break;

                //Beat two
                case "2":
                    if (_2 == 0)
                        _2 = 1;
                    else
                        _2 = 0;
                    break;

                case "2e":
                    if (_2e == 0)
                        _2e = 1;
                    else
                        _2e = 0;
                    break;

                case "2and":
                    if (_2and == 0)
                        _2and = 1;
                    else
                        _2and = 0;
                    break;

                case "2a":
                    if (_2a == 0)
                        _2a = 1;
                    else
                        _2a = 0;
                    break;

                //Beat three
                case "3":
                    if (_3 == 0)
                        _3 = 1;
                    else
                        _3 = 0;
                    break;

                case "3e":
                    if (_3e == 0)
                        _3e = 1;
                    else
                        _3e = 0;
                    break;

                case "3and":
                    if (_3and == 0)
                        _3and = 1;
                    else
                        _3and = 0;
                    break;

                case "3a":
                    if (_3a == 0)
                        _3a = 1;
                    else
                        _3a = 0;
                    break;

                //Beat four
                case "4":
                    if (_4 == 0)
                        _4 = 1;
                    else
                        _4 = 0;
                    break;

                case "4e":
                    if (_4e == 0)
                        _4e = 1;
                    else
                        _4e = 0;
                    break;

                case "4and":
                    if (_4and == 0)
                        _4and = 1;
                    else
                        _4and = 0;
                    break;

                case "4a":
                    if (_4a == 0)
                        _4a = 1;
                    else
                        _4a = 0;
                    break;
            }
        }

        //Output strum duration as a short.
        public int GetStrumDuration()
        {
            return strumDuration;
        }

        //Output time signature as a shor.
        public int GetBeatsPerMeasure()
        {
            return beatsPerMeasure;
        }

        //Output strum pattern as a string.
        public string GetPattern(char delimiter)
        {
            string PatternOut = "";

            //Beat one
            PatternOut += _1 + delimiter.ToString();
            PatternOut += _1e + delimiter.ToString();
            PatternOut += _1and + delimiter.ToString();
            PatternOut += _1a + delimiter.ToString();

            //Beat two
            PatternOut += _2 + delimiter.ToString();
            PatternOut += _2e + delimiter.ToString();
            PatternOut += _2and + delimiter.ToString();
            PatternOut += _2a + delimiter.ToString();

            //Beat three
            PatternOut += _3 + delimiter.ToString();
            PatternOut += _3e + delimiter.ToString();
            PatternOut += _3and + delimiter.ToString();
            PatternOut += _3a + delimiter.ToString();

            //Beat four
            PatternOut += _4 + delimiter.ToString();
            PatternOut += _4e + delimiter.ToString();
            PatternOut += _4and + delimiter.ToString();
            PatternOut += _4a + delimiter.ToString();

            return PatternOut;
        }

        //Load a file
        public void LoadPattern(string newPattern, char delimiter)
        {
            string[] newPatternArray = newPattern.Split(delimiter);

            //Load title and author.
            title = newPatternArray[0];
            author = newPatternArray[1];

            //Load beats per measure.
            if (newPatternArray[2] == "3" || newPatternArray[2] == "4") //Checks that the value is valid.
                beatsPerMeasure = Convert.ToInt32(newPatternArray[2]);
            else if (Convert.ToInt32(newPatternArray[2]) < 3)
            {
                //Default to 3/4 time if the corrutp value is less than 3.
                MessageBox.Show("Time signature is corrupted. Defaulting to 3/4.", "Corrupted Data");
                beatsPerMeasure = 3;
            }
            else if (Convert.ToInt32(newPatternArray[2]) > 4)
            {
                //Default to 4/4 time is the corrupt value is more than 4.
                MessageBox.Show("Time signature is corrupted. Defaulting to 4/4.", "Corrupted Data");
                beatsPerMeasure = 4;
            }

            //Load strum duration.
            if (newPatternArray[3] == "8" || newPatternArray[3] == "16")//Checks that the value is valid.
                strumDuration = Convert.ToInt32(newPatternArray[3]);
            else if (Convert.ToInt32(newPatternArray[3]) < 16 && Convert.ToInt32(newPatternArray[3]) != 8)
            {
                //Default to 8th notes if the corrupt value is less than 16.
                MessageBox.Show("Strum duration is corrupted. Defaulting to 8th notes.", "Corrupted Data");
                strumDuration = 8;
            }
            else if (Convert.ToInt32(newPatternArray) > 16)
            {
                //Default to 16th notes if the corrupt value is more than 16.
                MessageBox.Show("Strum duration is corrupted. Defaulting to 16th notes.", "Corrupted Data");
                strumDuration = 16;
            }

            //Load beat info.
            //If there are any corrupted strum values, make them equal to 0 (no strum on that beat.).
            bool corruptedStrum = false;

            //Check and "fix" any strum value errors.
            for (int x = 4; x <= 19; x++)
            {
                if (Convert.ToInt32(newPatternArray[x]) > 1)
                {
                    newPatternArray[x] = "0"; //Set the strum value to "off"
                    corruptedStrum = true; //We use this value to know whether we should alert the user of a corruption.
                }

            }

            //Tell the user that some strum values were corrupted.
            if (corruptedStrum)
                MessageBox.Show("Some strum values atre corrupted. Defaulting them to off (no strum).", "Corrupted Data");

            //Fill the strum info into the strumPattern class' properties.
            _1 = Convert.ToInt32(newPatternArray[4]);
            _1e = Convert.ToInt32(newPatternArray[5]);
            _1and = Convert.ToInt32(newPatternArray[6]);
            _1a = Convert.ToInt32(newPatternArray[7]);

            _2 = Convert.ToInt32(newPatternArray[8]);
            _2e = Convert.ToInt32(newPatternArray[9]);
            _2and = Convert.ToInt32(newPatternArray[10]);
            _2a = Convert.ToInt32(newPatternArray[11]);

            _3 = Convert.ToInt32(newPatternArray[12]);
            _3e = Convert.ToInt32(newPatternArray[13]);
            _3and = Convert.ToInt32(newPatternArray[14]);
            _3a = Convert.ToInt32(newPatternArray[15]);

            _4 = Convert.ToInt32(newPatternArray[16]);
            _4e = Convert.ToInt32(newPatternArray[17]);
            _4and = Convert.ToInt32(newPatternArray[18]);
            _4a = Convert.ToInt32(newPatternArray[19]);
        }
    }
}